package com.xintonghua.bussiness.ui.fragment.client.user;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.xintonghua.base.widget.ShowAllListView;
import com.xintonghua.bussiness.R;
import com.xintonghua.bussiness.base.BaseActivity;
import com.xintonghua.bussiness.bean.Contant;
import com.xintonghua.bussiness.bean.HairdressingTestBean;
import com.xintonghua.bussiness.bean.MeiRongTestContentBean;
import com.xintonghua.bussiness.bean.ResultScore;
import com.xintonghua.bussiness.util.AbstractBaseAdapter;
import com.xintonghua.bussiness.util.JsonUtil;
import com.xintonghua.bussiness.util.JumpUtils;
import com.xintonghua.bussiness.util.MyState;
import com.xintonghua.bussiness.util.MyUtils;
import com.xintonghua.bussiness.util.ViewHolder;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MeiRongTestContentActivity extends BaseActivity {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.sl_listview)
    ShowAllListView slListview;
    Contant contantbean;
    HairdressingTestBean bean;
    @BindView(R.id.tv_ti_number)
    TextView tvTiNumber;
    @BindView(R.id.btn_next_ti)
    Button btnNextTi;

    double zongfen = 0;

    ArrayList<Integer> xuanxianglist = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mei_rong_test_content);
        ButterKnife.bind(this);
//        bun.putSerializable("data0", contantbean);
//        bun.putSerializable("data1", bean);
        contantbean = (Contant) JumpUtils.getSerializable(this, "data0");
        bean = (HairdressingTestBean) JumpUtils.getSerializable(this, "data1");
        zongfen = JumpUtils.getDouble(this, "zongfen", 0);
        xuanxianglist = getIntent().getIntegerArrayListExtra("xuanxianglist");

        if (MyUtils.isNullOrEmpty(xuanxianglist)) {
            xuanxianglist = new ArrayList<>();
        }
        onSimpleActionBar();
        setSimpleActionBar("美容测试");


        list_bean = (List<MeiRongTestContentBean>) JumpUtils.getSerializable(this, "data2");// bun.putSerializable("data2", (Serializable)list_bean);

        dijiti = JumpUtils.getInteger(this, "dijiti", -1);

        if (dijiti != -1) {
            tvTiNumber.setText("第" + dijiti + "题");
            if (!MyUtils.isNullOrEmpty(list_bean)) {
                initListView();
            }
        } else {
            dijiti = 1;
            tvTiNumber.setText("第" + dijiti + "题");
            getNetData();
        }

    }

    List<MeiRongTestContentBean> list_bean;

    @Override
    public void dataBack(Object data, int order) {
        super.dataBack(data, order);
        try {
            switch (order) {
                case MyState.requestOne:
                    list_bean = JsonUtil.getListByJsonString(data.toString(), MeiRongTestContentBean.class);
                    Log.i("aaa", "" + list_bean.toString());
                    initListView();
                    break;
                case MyState.requestTwo:
                    ResultScore score = JSONObject.parseObject((String) data, ResultScore.class);
                    Bundle bun = new Bundle();
                    bun.putSerializable("data0", contantbean);
                    bun.putSerializable("data1", bean);
                    bun.putSerializable("data3", score);
                    bun.putSerializable("data2", (Serializable) list_bean);

                    try {
                        zongfen += Double.parseDouble(list_bean.get(dijiti - 1).getChoice().get(selectedpositon).getScore());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    bun.putDouble("zongfen", zongfen);
                    JumpUtils.jumptoB(MeiRongTestContentActivity.this, DialogMeiYunTestJieGuoActivity.class, bun);
                    finish();
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.i("aaa", "出错");
        }
    }

    int selectedpositon = -1;

    int dijiti = 1;

    public void initListView() {
        tvTitle.setText("" + list_bean.get(dijiti - 1).getTitle());
        slListview.setAdapter(new AbstractBaseAdapter<MeiRongTestContentBean.ChoiceBean>(list_bean.get(dijiti - 1).getChoice()) {
            @Override
            public View getAdapterViewAtPosition(final int position, View convertView, ViewGroup parent) {

                if (convertView == null) {
                    convertView = LayoutInflater.from(MeiRongTestContentActivity.this).inflate(R.layout.adapter_item_activity_meirong_test_content, null);
                }
                MeiRongTestContentBean.ChoiceBean bean = getItem(position);
                TextView tv_type = ViewHolder.get(convertView, R.id.tv_type);//LayoutInflater.from(HairdressingTestActivity.this).inflate(R.layout.activity_customers, null);
                ImageView iv_gou = ViewHolder.get(convertView, R.id.iv_gou);
                tv_type.setText("" + bean.getChoose() + "、" + bean.getValue());

                if (selectedpositon == position) {
                    iv_gou.setSelected(true);
                } else {
                    iv_gou.setSelected(false);
                }
                FrameLayout fl_content = ViewHolder.get(convertView, R.id.fl_content);
                fl_content.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        selectedpositon = position;
                        notifyDataSetChanged();
                    }
                });
                if (position == 0) {
                    fl_content.setBackgroundResource(R.drawable.layer_list_selector_layer_top);
                } else if (position == list_bean.get(dijiti - 1).getChoice().size() - 1) {
                    fl_content.setBackgroundResource(R.drawable.layer_list_selector_bottom);
                } else {
                    fl_content.setBackgroundResource(R.drawable.layer_list_selector_layer_center);
                }
                return convertView;
            }
        });

    }


    public void getNetData() {
        httpCent.getBeautyTitle(bean.getId(), this, MyState.requestOne);
    }

    @OnClick(R.id.btn_next_ti)
    public void onViewClicked() {
        if (selectedpositon == -1) {
            MyUtils.mToast(this, "请选择一项");
            return;
        }
//        contantbean = (Contant) JumpUtils.getSerializable(this, "data0");
//        bean = (HairdressingTestBean) JumpUtils.getSerializable(this, "data1");
        Bundle bun = new Bundle();
        bun.putSerializable("data0", contantbean);
        bun.putSerializable("data1", bean);
        bun.putSerializable("data2", (Serializable) list_bean);
        try {
            zongfen += Double.parseDouble(list_bean.get(dijiti - 1).getChoice().get(selectedpositon).getScore());
        } catch (Exception e) {
            e.printStackTrace();
        }

        bun.putDouble("zongfen", zongfen);
        xuanxianglist.add(selectedpositon);
        bun.putIntegerArrayList("xuanxianglist", xuanxianglist);
        if (dijiti < list_bean.size()) {
            dijiti++;
            bun.putInt("dijiti", dijiti);
            JumpUtils.jumptoB(MeiRongTestContentActivity.this, MeiRongTestContentActivity.class, bun);
            finish();
        } else {
            httpCent.beautyType(bean.getId(), zongfen + "", this, MyState.requestTwo);

        }

    }
}
